const {Blog} = require("../model");
const {delComment} = require("./commentServer")
const {upTypesum} = require('./blogTypeServer')
const {setR,getR,delDB,caseTime, delR} = require('./redisServe');
const { getType } = require('../routerConfig')
function isblog(blog,isUp = false) {
    const obj1 ={ 
        htmlContent: "",
        blogType: '', 
        title: '', 
        thums: '', 
        descrition: ''
    }
    const obj2 ={ 
        htmlContent: "",
        blogType: '', 
        title: '', 
        thums: '', 
        descrition: '',
        toch: ''
    }
    let result = true
    const obj = isUp ? obj2 : obj1
    for(const p in obj) {
        if(!blog[p]){
           result = `${p}不能为空`
           break
        } 
    }
    return result
}

exports.addBlog = async (blog)=>{
    const isb = isblog(blog)
   if(typeof isb === 'string'){
      return isb
    }else{

        const result = await Blog.create(blog);
        await delDB(1)
        await upTypesum(blog.blogType,+1)
        return result;
    }
}
 
exports.upBlog = async (_id, htmlContent,blogType, title, thums, descrition, toch)=> {
    const isb = isblog({
        _id, htmlContent,blogType, title, thums, descrition, toch
    },true)

    if(typeof isb === 'string'){
       return isb
     }else{
       const temp = await Blog.findById(_id);
       const result = await Blog.updateOne({_id},{
            $set:{
                htmlContent,
                blogType, 
                title, 
                thums, 
                descrition, 
                toch
            }
       })
       await delR(2,_id)
       await upTypesum(temp.blogType,-1)
       await upTypesum(blogType,+1)
       await delDB(1)
       return result
    }

  
}

exports.getBlogDetalis = async (_id)=>{
    if(!_id) return "_Id 为NUll";
    
    const Cresult = await getR(2,_id);
    upBlogClickSum(_id);
    if(Cresult) return  JSON.parse(Cresult);

    const result = await Blog.findById(_id);

     setR(2,_id,JSON.stringify(result),{EX:caseTime});

    return result;
}

exports.delBlog = async (_Id,name)=>{
   
    if(!_Id) return '_Id为null'
    const result = await Blog.deleteOne({_id:_Id});
     await delDB(1)
     await delComment(_Id)
     await upTypesum(name,-1)
    return result

}
exports.getBlogList = async (origin,filters={},htmlContent=0,toch =0,limt = 10 ,skip=1)=>{
    try {
        const Cresult = await getR(1,origin);

        if(Cresult) return JSON.parse(Cresult);
        let n = (skip-1)*limt;
        const result =  await Blog.find(filters,{
                            htmlContent,
                            toch
                        })
                        .skip(n)
                        .limit(limt);
                    
        const count = await Blog.find(filters)
                        .count();
        setR(1,origin,JSON.stringify({result,count}),{EX:caseTime});
        
        return {result,count};

    } catch(e) {

        console.log(e)

    }
    
}

exports.findBlog =async (_id)=>{

    const result = await Blog.findById(_id);

     if(result) return true;
     else return false;


}

async function upBlogClickSum(_id){
    await Blog.updateOne({
        _id
    },{
        $inc:{
            clickSum:+1
        }
    })
}